﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace VRP_Saving_Final
{

    class RouteWrite   //this writes the output file
    {
        public static void Outputroutes(List<List<int>> list, double time, float objective, string inputfile)     //takes in the list of routes, the time, the objective cost, and the input file name
        {
            string path = inputfile + ".opt";                           //creates a output file name using inputfile's name
            string linetext = "";                                       //creates a string empty variable

            using (StreamWriter sw = new StreamWriter(path))            //initializes to write to path
            {
             
                int k = 1;                                      //counter initialized
                string kstring = "";                            //kstring is a string variable that will hold the converted counter k 

                foreach (var sublist in list)                   //This goes through each route in the list of routes
                {

                    foreach (var value in sublist)              //This sub goes through, for a particular route in the list, each node visited on that route
                    {


                        {
                            linetext += value.ToString() + " ";     //takes each node's id on an individual route, converts to string, and adds it to the current line for output
                        }

                    }

                    if (linetext != "")                             //if there was an empty route being held on the list, we don't want to output it
                    {
                        kstring = k.ToString();                     //convert the counter to a string
                        sw.Write("Route #" + kstring + ": ");       //output the route's number
                        sw.WriteLine(linetext);                     //write the route that we converted to string above
                        linetext = "";                              //clear the string linetext for the next route we will convert for output
                        k = k + 1;                                  //increment the counter
                    }

                }

                string watchstring = time.ToString();               //convert the clock time to string
                sw.WriteLine("Cost " + objective);                  //take the objective value cost and output it on it's own line
                sw.WriteLine("Time " + watchstring + " ms");        //take the clock time and output it on it's own line
            }
        }
    }
}
